class SearchController < BaseController
  
  #【参数】
  #【返回值】
  #【注意】
  #【作者】 by ljl 2012.08.29
  def index
    if request.xhr?
      json = []
      pins = Pin.find(:all, 
                      :conditions => ["pins.description like ?", "%#{params[:q]}%"],
                      :order => "pins.created_at desc, pins.id desc", 
                      :limit => APP_CONFIG[:page][:home_page], 
                      :offset => (params[:page].to_i - 1) * APP_CONFIG[:page][:home_page]
      )
      pp '************'
      pp pins
      pins.each do |pin|
        user = pin.user
        user_pin = UserPin.find_by_user_id_and_pin_id(session[:user], pin.id)
        p = {:id => pin.id,
             :board_id => pin.board.id,
             :board_title => pin.board.name,
             :user => {:id => user.id,
                       :name => user.name},
             :user_id => user.id,
             :media => pin.media,
             :description => pin.description,
             :link_count => pin.user_pins.count,
             :comment_count => pin.comments.count,
             :is_like  => true,
             :like_action => "",
             :like_btn => ""
        }
        if logged_in?
          p[:is_like] = current_user.id == user.id ? false : true 
        end
        if user_pin.present?
          p[:like_btn] = user_pin.like ? "取消喜欢" : "<em></em>喜欢"
          p[:like_action] = "unlike"
        else
          p[:like_btn] = "<em></em>喜欢"
          p[:like_action] = "like"
        end
        json << p
      end
      render :json => json.to_json
    else
      return
    end
  end
  
end
